 package handling.world;

 import client.MapleCharacter;
 import client.inventory.Item;
 import handling.Auction.AuctionServer;
 import handling.cashshop.CashShopServer;
 import handling.channel.ChannelServer;
 import handling.channel.PlayerStorage;
 import handling.world.guild.MapleBBSThread;
 import handling.world.guild.MapleGuild;
 import handling.world.guild.MapleGuildCharacter;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import tools.Pair;
 import tools.packet.GuildPacket;










 public class WorldGuildService
 {
/*  33 */   private static final Logger log = LogManager.getLogger(WorldGuildService.class.getName());
   private final Map<Integer, MapleGuild> guildList;
   private final ReentrantReadWriteLock lock;

   private WorldGuildService() {
/*  38 */     log.info("正在启动[WorldGuildService]");
/*  39 */     this.lock = new ReentrantReadWriteLock();
/*  40 */     this.guildList = new LinkedHashMap<>();
   }

   public static WorldGuildService getInstance() {
/*  44 */     return SingletonHolder.instance;
   }

   public void addLoadedGuild(MapleGuild guild) {
/*  48 */     if (guild.isProper()) {
/*  49 */       this.guildList.put(Integer.valueOf(guild.getId()), guild);
     }
   }




   public int createGuild(int leaderId, String name) {
/*  57 */     return MapleGuild.createGuild(leaderId, name);
   }

   public MapleGuild getGuild(int guildId) {
/*  61 */     MapleGuild ret = null;
/*  62 */     this.lock.readLock().lock();
     try {
/*  64 */       ret = this.guildList.get(Integer.valueOf(guildId));
     } finally {
/*  66 */       this.lock.readLock().unlock();
     }
/*  68 */     if (ret == null) {
/*  69 */       this.lock.writeLock().lock();
       try {
/*  71 */         ret = new MapleGuild(guildId);
/*  72 */         if (ret == null || ret.getId() <= 0 || !ret.isProper()) {
/*  73 */           return null;
         }
/*  75 */         this.guildList.put(Integer.valueOf(guildId), ret);
       } finally {
/*  77 */         this.lock.writeLock().unlock();
       }
     }
/*  80 */     return ret;
   }

   public MapleGuild getGuildByName(String guildName) {
/*  84 */     this.lock.readLock().lock();
     try {
/*  86 */       for (MapleGuild guild : this.guildList.values()) {
/*  87 */         if (guild.getName().equalsIgnoreCase(guildName)) {
/*  88 */           return guild;
         }
       }
/*  91 */       return null;
     } finally {
/*  93 */       this.lock.readLock().unlock();
     }
   }

   public MapleGuild getGuild(MapleCharacter chr) {
/*  98 */     return getGuild(chr.getGuildId());
   }




   public void setGuildMemberOnline(MapleGuildCharacter guildMember, boolean isOnline, int channel) {
/* 105 */     MapleGuild guild = getGuild(guildMember.getGuildId());
/* 106 */     if (guild != null) {
/* 107 */       guild.setOnline(guildMember.getId(), isOnline, channel);
     }
   }




   public void guildPacket(int guildId, byte[] message) {
/* 115 */     MapleGuild guild = getGuild(guildId);
/* 116 */     if (guild != null) {
/* 117 */       guild.broadcast(message);
     }
   }




   public int addGuildMember(MapleGuildCharacter guildMember) {
/* 125 */     MapleGuild guild = getGuild(guildMember.getGuildId());
/* 126 */     if (guild != null) {
/* 127 */       return guild.addGuildMember(guildMember);
     }
/* 129 */     return 0;
   }

   public int addGuildMember(int guildId, int chrId) {
/* 133 */     MapleGuild guild = getGuild(guildId);
/* 134 */     if (guild != null) {
/* 135 */       return guild.addGuildMember(chrId);
     }
/* 137 */     return 0;
   }




   public int addGuildApplyMember(MapleGuildCharacter guildMember) {
/* 144 */     MapleGuild guild = getGuild(guildMember.getGuildId());
/* 145 */     if (guild != null) {
/* 146 */       return guild.addGuildApplyMember(guildMember);
     }
/* 148 */     return 0;
   }




   public int denyGuildApplyMember(int guildId, int chrId) {
/* 155 */     MapleGuild guild = getGuild(guildId);
/* 156 */     if (guild != null) {

/* 158 */       guild.denyGuildApplyMember(chrId);
/* 159 */       int ch = WorldFindService.getInstance().findChannel(chrId);
/* 160 */       if (ch < 0) {
/* 161 */         return 0;
       }
/* 163 */       MapleCharacter player = getStorage(ch).getCharacterById(chrId);
/* 164 */       if (player == null) {
/* 165 */         return 0;
       }
/* 167 */       player.getClient().announce(GuildPacket.DenyGuildApply(chrId));
/* 168 */       return 1;
     }
/* 170 */     return 0;
   }




   public void leaveGuild(MapleGuildCharacter guildMember) {
/* 177 */     MapleGuild guild = getGuild(guildMember.getGuildId());
/* 178 */     if (guild != null) {
/* 179 */       guild.leaveGuild(guildMember);
     }
   }







   public void guildChat(int gid, String name, int cid, String msg) {
/* 193 */     guildChat(gid, name, cid, msg, null);
   }

   public void guildChat(int gid, String name, int cid, String msg, Item item) {
/* 197 */     MapleGuild g = getGuild(gid);
/* 198 */     if (g != null) {
/* 199 */       g.guildChat(name, cid, msg, item);
     }
   }




   public void changeRank(int guildId, int chrId, int newRank) {
/* 207 */     MapleGuild guild = getGuild(guildId);
/* 208 */     if (guild != null) {
/* 209 */       guild.changeRank(chrId, newRank);
     }
   }




   public void expelMember(MapleGuildCharacter initiator, String name, int chrId) {
/* 217 */     MapleGuild guild = getGuild(initiator.getGuildId());
/* 218 */     if (guild != null) {
/* 219 */       guild.expelMember(initiator, name, chrId);
     }
   }




   public void setGuildNotice(int guildId, String notice) {
/* 227 */     MapleGuild guild = getGuild(guildId);
/* 228 */     if (guild != null) {
/* 229 */       guild.setGuildNotice(notice);
     }
   }




   public void setGuildLeader(int guildId, int chrId) {
/* 237 */     MapleGuild guild = getGuild(guildId);
/* 238 */     if (guild != null) {
/* 239 */       guild.changeGuildLeader(chrId);
     }
   }




   public int getSkillLevel(int guildId, int skillId) {
/* 247 */     MapleGuild guild = getGuild(guildId);
/* 248 */     if (guild != null) {
/* 249 */       return guild.getSkillLevel(skillId);
     }
/* 251 */     return 0;
   }




   public boolean purchaseSkill(int guildId, int skillId, String name, int chrId) {
/* 258 */     MapleGuild guild = getGuild(guildId);
/* 259 */     return (guild != null && guild.purchaseSkill(skillId, name, chrId));
   }




   public boolean activateSkill(int guildId, int skillId, String name) {
/* 266 */     MapleGuild guild = getGuild(guildId);
/* 267 */     return (guild != null && guild.activateSkill(skillId, name));
   }




   public void memberLevelJobUpdate(MapleGuildCharacter guildMember) {
/* 274 */     MapleGuild guild = getGuild(guildMember.getGuildId());
/* 275 */     if (guild != null) {
/* 276 */       guild.memberLevelJobUpdate(guildMember);
     }
   }




   public void changeRankTitle(int guildId, String[] ranks) {
/* 284 */     MapleGuild guild = getGuild(guildId);
/* 285 */     if (guild != null) {
/* 286 */       guild.changeRankTitle(ranks);
     }
   }




   public void setGuildEmblem(int guildId, short bg, byte bgcolor, short logo, byte logocolor) {
/* 294 */     MapleGuild guild = getGuild(guildId);
/* 295 */     if (guild != null) {
/* 296 */       guild.setGuildEmblem(bg, bgcolor, logo, logocolor);
     }
   }




   public void disbandGuild(int guildId) {
/* 304 */     MapleGuild guild = getGuild(guildId);
/* 305 */     this.lock.writeLock().lock();
     try {
/* 307 */       if (guild != null) {
/* 308 */         guild.disbandGuild();
/* 309 */         this.guildList.remove(Integer.valueOf(guildId));
       }
     } finally {
/* 312 */       this.lock.writeLock().unlock();
     }
   }




   public void deleteGuildCharacter(int guildId, int charId) {
/* 320 */     MapleGuild guild = getGuild(guildId);
/* 321 */     if (guild != null) {
/* 322 */       MapleGuildCharacter mc = guild.getMGC(charId);
/* 323 */       if (mc != null) {
/* 324 */         if (mc.getGuildRank() > 1) {
/* 325 */           guild.leaveGuild(mc);
         } else {
/* 327 */           guild.disbandGuild();
         }
       }
     }
   }




   public boolean increaseGuildCapacity(int guildId, boolean b) {
/* 337 */     MapleGuild guild = getGuild(guildId);
/* 338 */     return (guild != null && guild.increaseCapacity(b));
   }




   public void gainGP(int guildId, int amount) {
/* 345 */     MapleGuild guild = getGuild(guildId);
/* 346 */     if (guild != null) {
/* 347 */       guild.gainGP(amount);
     }
   }

   public void gainGP(int guildId, int amount, int chrId) {
/* 352 */     MapleGuild guild = getGuild(guildId);
/* 353 */     if (guild != null) {
/* 354 */       guild.gainGP(amount, false, chrId);
     }
   }

   public int getGP(int guildId) {
/* 359 */     MapleGuild guild = getGuild(guildId);
/* 360 */     if (guild != null) {
/* 361 */       return guild.getGP();
     }
/* 363 */     return 0;
   }

   public int getInvitedId(int guildId) {
/* 367 */     MapleGuild guild = getGuild(guildId);
/* 368 */     if (guild != null) {
/* 369 */       return guild.getInvitedId();
     }
/* 371 */     return 0;
   }

   public void setInvitedId(int guildId, int inviteId) {
/* 375 */     MapleGuild guild = getGuild(guildId);
/* 376 */     if (guild != null) {
/* 377 */       guild.setInvitedId(inviteId);
     }
   }

   public int getGuildLeader(int guildName) {
/* 382 */     MapleGuild guild = getGuild(guildName);
/* 383 */     if (guild != null) {
/* 384 */       return guild.getLeaderId();
     }
/* 386 */     return 0;
   }

   public int getGuildLeader(String guildName) {
/* 390 */     MapleGuild guild = getGuildByName(guildName);
/* 391 */     if (guild != null) {
/* 392 */       return guild.getLeaderId();
     }
/* 394 */     return 0;
   }

   public void save() {
/* 398 */     System.out.println("正在保存家族数据...");
/* 399 */     this.lock.writeLock().lock();
     try {
/* 401 */       for (MapleGuild guild : this.guildList.values()) {
/* 402 */         guild.writeToDB(false);
       }
     } finally {
/* 405 */       this.lock.writeLock().unlock();
     }
   }




   public List<MapleBBSThread> getBBS(int guildId) {
/* 413 */     MapleGuild guild = getGuild(guildId);
/* 414 */     if (guild != null) {
/* 415 */       return guild.getBBS();
     }
/* 417 */     return null;
   }




   public int addBBSThread(int guildId, String title, String text, int icon, boolean bNotice, int posterId) {
/* 424 */     MapleGuild guild = getGuild(guildId);
/* 425 */     if (guild != null) {
/* 426 */       return guild.addBBSThread(title, text, icon, bNotice, posterId);
     }
/* 428 */     return -1;
   }




   public void editBBSThread(int guildId, int localthreadId, String title, String text, int icon, int posterId, int guildRank) {
/* 435 */     MapleGuild guild = getGuild(guildId);
/* 436 */     if (guild != null) {
/* 437 */       guild.editBBSThread(localthreadId, title, text, icon, posterId, guildRank);
     }
   }




   public void deleteBBSThread(int guildId, int localthreadId, int posterId, int guildRank) {
/* 445 */     MapleGuild guild = getGuild(guildId);
/* 446 */     if (guild != null) {
/* 447 */       guild.deleteBBSThread(localthreadId, posterId, guildRank);
     }
   }




   public void addBBSReply(int guildId, int localthreadId, String text, int posterId) {
/* 455 */     MapleGuild guild = getGuild(guildId);
/* 456 */     if (guild != null) {
/* 457 */       guild.addBBSReply(localthreadId, text, posterId);
     }
   }




   public void deleteBBSReply(int guildId, int localthreadId, int replyId, int posterId, int guildRank) {
/* 465 */     MapleGuild guild = getGuild(guildId);
/* 466 */     if (guild != null) {
/* 467 */       guild.deleteBBSReply(localthreadId, replyId, posterId, guildRank);
     }
   }




   public void changeEmblem(int guildId, int affectedPlayers, MapleGuild guild) {
/* 475 */     WorldBroadcastService.getInstance().sendGuildPacket(affectedPlayers, GuildPacket.guildEmblemChange(guildId, (short)guild.getLogoBG(), (byte)guild.getLogoBGColor(), (short)guild.getLogo(), (byte)guild.getLogoColor()), -1, guildId, false);
/* 476 */     setGuildAndRank(affectedPlayers, -1, -1, -1, -1);
   }
   public void setGuildAndRank(int chrId, int guildId, int rank, int contribution, int alliancerank) {
     boolean isDifferentGuild;
/* 480 */     int ch = WorldFindService.getInstance().findChannel(chrId);
/* 481 */     if (ch == -1) {
       return;
     }
/* 484 */     MapleCharacter player = getStorage(ch).getCharacterById(chrId);
/* 485 */     if (player == null) {
       return;
     }

/* 489 */     if (guildId == -1 && rank == -1) {
/* 490 */       isDifferentGuild = true;
     } else {
/* 492 */       isDifferentGuild = (guildId != player.getGuildId());
/* 493 */       player.setGuildId(guildId);
/* 494 */       player.setGuildRank((byte)rank);
/* 495 */       player.setGuildContribution(contribution);
/* 496 */       player.setAllianceRank((byte)alliancerank);
/* 497 */       player.saveGuildStatus();
     }
/* 499 */     if (isDifferentGuild && ch > 0) {
/* 500 */       player.getMap().broadcastMessage(player, GuildPacket.loadGuildName(player), false);
/* 501 */       player.getMap().broadcastMessage(player, GuildPacket.loadGuildIcon(player), false);
     }
   }

   public PlayerStorage getStorage(int channel) {
/* 506 */     if (channel == -20)
/* 507 */       return AuctionServer.getPlayerStorage(); 
/* 508 */     if (channel == -10) {
/* 509 */       return CashShopServer.getPlayerStorage();
     }
/* 511 */     return ChannelServer.getInstance(channel).getPlayerStorage();
   }

   public List<Pair<Integer, MapleGuild>> getGuildList() {
/* 515 */     List<Pair<Integer, MapleGuild>> gui = new ArrayList<>();
/* 516 */     for (Map.Entry<Integer, MapleGuild> g : this.guildList.entrySet()) {
/* 517 */       gui.add(new Pair(g.getKey(), g.getValue()));
     }
/* 519 */     return gui;
   }

   private static class SingletonHolder
   {
/* 524 */     protected static final WorldGuildService instance = new WorldGuildService();
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\WorldGuildService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */